package com.unohacha.qqj.fzdr.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.unohacha.qqj.fzdr.FindImageByImageActivity;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.ui.activity.supply.FabricCateActivity;
import com.unohacha.qqj.fzdr.ui.fragment.AfterPubMatchFragment;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import butterknife.BindView;

/**
 * 发布后匹配
 */
public class AfterPubMatchActivity extends BaseActivity {
    private IndicatorViewPager indicatorViewPager;
    private LayoutInflater inflate;
    ViewPager viewPager;
    Indicator indicator;
    int[] indicatorWidth = {130, 130};
    @BindView(R.id.common_back)
    ImageView commonBack;
    TextView mainHeadCamera;
    Bundle bundle;
    AfterPubMatchFragment afterPubMatchFragment;
    private String orderId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_after_pub;
    }

    @Override
    protected void initData() {
        super.initData();
        orderId = getIntent().getStringExtra("orderId");
        ImageView common_back = findViewById(R.id.common_back);
        TextView tvSearch = findViewById(R.id.main_head_camera);
        common_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                finishActivity(FabricCateActivity.class);
            }
        });
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpToOtherActivity(AfterPubMatchActivity.this, FindImageByImageActivity.class);
            }
        });
        viewPager = findViewById(R.id.fragment_tabmain_viewPager);
        indicator = findViewById(R.id.fragment_tabmain_indicator);
        mainHeadCamera = findViewById(R.id.main_head_camera);
        mainHeadCamera.setVisibility(View.GONE);
        ViewPagerSetting();
    }

    /**
     * 滑动界面tab标题的配置
     */
    private void ViewPagerSetting() {
        final ColorBar colorBar = new ColorBar(this, getResources().getColor(R.color.black), 5);
        colorBar.setWidth(130);
        // 指示条
        indicator.setScrollBar(colorBar);
        // 未选中字体大小
        float unSelectSize = 14;
        // 选中字体大小
        float selectSize = unSelectSize * 1.0f;

        // 未选中的颜色
        int unSelectColor = Color.parseColor("#bbbbbb");
        // 选中时的颜色
        int selectColor = getResources().getColor(R.color.black);

        // 设置tab字体的变化
        indicator.setOnTransitionListener(new OnTransitionTextListener(
                selectSize, unSelectSize, selectColor, unSelectColor));

        // 禁止viewpager的滑动事件
        // viewPager.setCanScroll(false);
        // 设置viewpager保留界面不重新加载的页面数量
        viewPager.setOffscreenPageLimit(2);
        // 默认是1,，自动预加载左右两边的界面。设置viewpager预加载数为0。只加载加载当前界面。
        // viewPager.setPrepareNumber(0);

        indicatorViewPager = new IndicatorViewPager(indicator, viewPager);

        // 设置适配器
        indicatorViewPager
                .setAdapter(new MyAdapter(getSupportFragmentManager()));

        indicatorViewPager
                .setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {

                    @Override
                    public void onIndicatorPageChange(int preItem,
                                                      int currentItem) {
                        if (indicatorWidth == null) {
                            return;
                        } else {
                            if (currentItem == 0) {
                                colorBar.setWidth(indicatorWidth[0]);
                            } else if (currentItem == 1) {
                                colorBar.setWidth(indicatorWidth[1]);
                            }
                        }

                    }
                });
        // indicatorViewPager.setCurrentItem(1, true);

    }

    /*
     * 适配器
     */
    private class MyAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        private String[] tabName = new String[]{"有效信息", "过期信息"};
        public MyAdapter(FragmentManager arg0) {
            super(arg0);
            inflate = LayoutInflater.from(AfterPubMatchActivity.this);
        }

        @Override
        public int getCount() {
            return tabName.length;
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {

            if (convertView == null)
                convertView = inflate.inflate(R.layout.indicater_tabtext, container, false);

            TextView textView = (TextView) convertView;
            textView.setText(tabName[position]);
            int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            textView.measure(spec, spec);
            indicatorWidth[position] = textView.getMeasuredWidth();
            return convertView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            // 填充Fragment界面,需要与tab数量相等
            switch (position) {
                case 0:
                    afterPubMatchFragment = new AfterPubMatchFragment();
                    bundle = new Bundle();
                    bundle.putString("type", "0");
                    bundle.putString("orderId", orderId);
                    afterPubMatchFragment.setArguments(bundle);
                    return afterPubMatchFragment;

                case 1:
                    afterPubMatchFragment = new AfterPubMatchFragment();
                    bundle = new Bundle();
                    bundle.putString("type", "1");
                    bundle.putString("orderId", orderId);
                    afterPubMatchFragment.setArguments(bundle);
                    return afterPubMatchFragment;
                default:
                    afterPubMatchFragment = new AfterPubMatchFragment();
                    bundle = new Bundle();
                    bundle.putString("type", "0");
                    bundle.putString("orderId", orderId);
                    afterPubMatchFragment.setArguments(bundle);
                    return afterPubMatchFragment;
            }

        }

    }
}
